package OBS;
import Databasehandler.DBCommunicator;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;

/**
 *
 * @author Daniel Koch
 */
public class OBSUser {
    protected DBCommunicator DBC;

    /**
     * 
     * @param DBC
     */
    public OBSUser(DBCommunicator DBC) {
        this.DBC = DBC;
    }

    //<editor-fold desc="Kunde">

    /**
     * Hent aktuelle kunder
     * @return
     */
    public Customer[] getCurrentCustomers() {
        try {
            ArrayList<Customer> al = new ArrayList<Customer>();
            ResultSet rs = this.DBC.getCurrentCustomers();
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                BatchReportSystem[] batchReportSystems = getCurrentCustomerBatchReportSystems(id);
                OBSReportSubscriber[] OBSReportSubscribers = getCurrentCustomerOBSReportSubscribers(id);
                al.add(new Customer(id, name, batchReportSystems, OBSReportSubscribers));
            }
            Customer[] a = new Customer[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return a;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //</editor-fold>

    //<editor-fold desc="Batchrapportsystem">

    /**
     * Hent aktuelle batchrapportsystemer for en kunde
     * @param customerID
     * @return
     */
    public BatchReportSystem[] getCurrentCustomerBatchReportSystems(int customerID) {
        try {
            ArrayList<BatchReportSystem> al = new ArrayList<BatchReportSystem>();
            ResultSet rs = this.DBC.getCurrentCustomerBatchReportSystems(customerID);
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                BatchReportInterval batchReportInterval = getBatchReportInterval(rs.getInt(3));
                BatchReport[] batchReports = getBatchReportSystemBatchReports(id);
                BatchReport[] currentBatchReports = getCurrentBatchReportSystemBatchReports(id);
                al.add(new BatchReportSystem(id, name, batchReportInterval, batchReports, currentBatchReports));
            }
            BatchReportSystem[] a = new BatchReportSystem[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return a;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Hent aktuelle batchrapportsystemer for en abonnent
     * @param OBSReportSubscriberID
     * @return
     */
    public BatchReportSystem[] getCurrentOBSReportSubscriberOBSReportSubscriptions(int OBSReportSubscriberID) {
        try {
            ArrayList<BatchReportSystem> al = new ArrayList<BatchReportSystem>();
            ResultSet rs = this.DBC.getCurrentOBSReportSubscriberOBSReportSubscriptions(OBSReportSubscriberID);
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                BatchReportInterval batchReportInterval = getBatchReportInterval(rs.getInt(3));
                BatchReport[] batchReports = getBatchReportSystemBatchReports(id);
                BatchReport[] currentBatchReports = getCurrentBatchReportSystemBatchReports(id);
                al.add(new BatchReportSystem(id, name, batchReportInterval, batchReports, currentBatchReports));
            }
            BatchReportSystem[] a = new BatchReportSystem[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return a;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //</editor-fold>

    //<editor-fold desc="Abonnent">

    /**
     * Hent aktuelle abonnenter for en kunde
     * @param customerID
     * @return
     */
    public OBSReportSubscriber[] getCurrentCustomerOBSReportSubscribers(int customerID) {
        try {
            ArrayList<OBSReportSubscriber> al = new ArrayList<OBSReportSubscriber>();
            ResultSet rs = this.DBC.getCurrentCustomerOBSReportSubscribers(customerID);
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(3);
                String email = rs.getString(4);
                OBSReportSubscription OBSReportSubscription = getCurrentOBSReportSubscriberOBSReportSubscription(id);
                al.add(new OBSReportSubscriber(id, name, email, OBSReportSubscription));
            }
            OBSReportSubscriber[] a = new OBSReportSubscriber[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return a;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //</editor-fold>

    //<editor-fold desc="Abonnement">

    /**
     * Hent et abonnement for en abonnent
     * @param OBSReportSubscriberID
     * @return
     */
    public OBSReportSubscription getOBSReportSubscriberOBSReportSubscription(int OBSReportSubscriberID) {
        try {
            ArrayList<BatchReportSystem> al = new ArrayList<BatchReportSystem>();
            ResultSet rs = this.DBC.getOBSReportSubscriberOBSReportSubscriptions(OBSReportSubscriberID);
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                BatchReportInterval batchReportInterval = getBatchReportInterval(rs.getInt(3));
                BatchReport[] batchReports = getBatchReportSystemBatchReports(id);
                BatchReport[] currentBatchReports = getCurrentBatchReportSystemBatchReports(id);
                al.add(new BatchReportSystem(id, name, batchReportInterval, batchReports, currentBatchReports));
            }
            BatchReportSystem[] a = new BatchReportSystem[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return new OBSReportSubscription(a);
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Hent et aktuelt abonnement for en abonnent
     * @param OBSReportSubscriberID
     * @return
     */
    public OBSReportSubscription getCurrentOBSReportSubscriberOBSReportSubscription(int OBSReportSubscriberID) {
        try {
            ArrayList<BatchReportSystem> al = new ArrayList<BatchReportSystem>();
            ResultSet rs = this.DBC.getCurrentOBSReportSubscriberOBSReportSubscriptions(OBSReportSubscriberID);
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                BatchReportInterval batchReportInterval = getBatchReportInterval(rs.getInt(3));
                BatchReport[] batchReports = getBatchReportSystemBatchReports(id);
                BatchReport[] currentBatchReports = getCurrentBatchReportSystemBatchReports(id);
                al.add(new BatchReportSystem(id, name, batchReportInterval, batchReports, currentBatchReports));
            }
            BatchReportSystem[] a = new BatchReportSystem[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return new OBSReportSubscription(a);
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //</editor-fold>

    //<editor-fold desc="Batchrapport">

    /**
     * Hent batchrapporter for et batchsystem
     * @param batchReportSystemID
     * @return
     */
    public BatchReport[] getBatchReportSystemBatchReports(int batchReportSystemID) {
        try {
            ArrayList<BatchReport> al = new ArrayList<BatchReport>();
            ResultSet rs = this.DBC.getBatchReportSystemBatchReports(batchReportSystemID);
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                GregorianCalendar batchStartTime = stringToDate(rs.getString(4));
                GregorianCalendar batchEndTime = stringToDate(rs.getString(5));
                BatchReportDetailSet[] batchReportDetailSets = getBatchReportDetailSets(id);
                Boolean success = rs.getBoolean(6);
                String statusMessage = rs.getString(7);
                GregorianCalendar timeRecieved = stringToDate(rs.getString(8));
                al.add(new BatchReport(id, name, timeRecieved, batchStartTime, batchEndTime, success, statusMessage, batchReportDetailSets));
            }
            BatchReport[] a = new BatchReport[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return a;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Hent aktuelle batchrapporter for et batchsystem
     * @param batchReportSystemID
     * @return
     */
    public BatchReport[] getCurrentBatchReportSystemBatchReports(int batchReportSystemID) {
        try {
            ArrayList<BatchReport> al = new ArrayList<BatchReport>();
            ResultSet rs = this.DBC.getCurrentBatchReportSystemBatchReports(batchReportSystemID);
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                GregorianCalendar batchStartTime = stringToDate(rs.getString(4));
                GregorianCalendar batchEndTime = stringToDate(rs.getString(5));
                BatchReportDetailSet[] batchReportDetailSets = getBatchReportDetailSets(id);
                Boolean success = rs.getBoolean(6);
                String statusMessage = rs.getString(7);
                GregorianCalendar timeRecieved = stringToDate(rs.getString(8));
                al.add(new BatchReport(id, name, timeRecieved, batchStartTime, batchEndTime, success, statusMessage, batchReportDetailSets));
            }
            BatchReport[] a = new BatchReport[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return a;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Hent batchrapportdetaljesæt for en batchrapport
     * @param batchReportID
     * @return
     */
    public BatchReportDetailSet[] getBatchReportDetailSets(int batchReportID) {
        try {
            ArrayList<BatchReportDetailSet> al = new ArrayList<BatchReportDetailSet>();
            ResultSet rs = this.DBC.getBatchReportDetailSets(batchReportID);
            while(rs.next()) {
                int id = rs.getInt(1);
                String detailSetName = rs.getString(3);
                int totalUpdates = rs.getInt(4);
                int totalCreates = rs.getInt(5);
                int totalDeletes = rs.getInt(6);
                int totalErrors = rs.getInt(7);
                int totalSkips = rs.getInt(8);
                al.add(new BatchReportDetailSet(id, detailSetName, totalUpdates, totalCreates, totalDeletes, totalErrors, totalSkips));
            }
            BatchReportDetailSet[] a = new BatchReportDetailSet[al.size()];
            for(int i = 0;i < a.length;i++) {
                a[i] = al.get(i);
            }
            return a;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //</editor-fold>

    //<editor-fold desc="Andet">

    /**
     * Hent et batchrapportinterval
     * @param batchReportIntervalID
     * @return
     */
    public BatchReportInterval getBatchReportInterval(int batchReportIntervalID) {
        try {
            ResultSet rs = this.DBC.getBatchReportInterval(batchReportIntervalID);
            rs.next();
            return new BatchReportInterval(rs.getInt(1), rs.getString(2));
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 
     * @param date
     * @return
     */
    private GregorianCalendar stringToDate(String date) {
        GregorianCalendar gc = new GregorianCalendar();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            gc.setTime(format.parse(date));
            return gc;
        }
        catch(Exception e) {
            e.printStackTrace();
            return new GregorianCalendar();
        }
    }

    //</editor-fold>
}
